﻿/*
 * 由SharpDevelop创建。
 * 用户： xuhh01
 * 日期: 2015/11/19
 * 时间: 15:08
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Windows;

namespace Listener
{
	/// <summary>
	/// Description of SerializationFile.
	/// </summary>
	public class SerializationFile
	{
		public SerializationFile() {	}
		
		public static bool SerializationXml<T>(string strFileFullPath, T t)
		{
			FileStream fs = File.Open(strFileFullPath, FileMode.OpenOrCreate);
			try {
					XmlSerializer sa = new XmlSerializer(t.GetType());
					sa.Serialize(fs, t);
				
			} catch (Exception e) {
				
				MessageBox.Show(e.Message, "错误");
				fs.Close();
				return false;
			}
			
			fs.Close();
			return true;
		}
		
		public static bool XmlDeserialization<T>(string strFileFullPath, ref T t)
		{
			if (File.Exists(strFileFullPath) == false) {
				return false;
			}
			
			FileStream fs = File.Open(strFileFullPath, FileMode.Open);
			try {
				XmlSerializer sa = new XmlSerializer(t.GetType());
				t = (T)sa.Deserialize(fs);
			} catch (Exception) {
				
				fs.Close();
				return false;
			}
			fs.Close();
			return true;
		}
	}
}
